package com.zt.ztyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.ztyh.dto.FundsCount;
import com.zt.ztyh.dto.FundsDto;
import com.zt.ztyh.dto.FundsUserDto;
import com.zt.ztyh.entity.User;
import com.zt.ztyh.entity.ZtyhFundsUser;
import com.zt.ztyh.exception.ZhentaoException;
import com.zt.ztyh.service.ZtyhFundsUserService;
import com.zt.ztyh.mapper.ZtyhFundsUserMapper;
import com.zt.ztyh.utils.IdWorker;
import com.zt.ztyh.vo.common.ResultCodeEnum;
import com.zt.ztyh.vo.funds.FundsUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
* @author JIASC
* @description 针对表【ztyh_funds_user】的数据库操作Service实现
* @createDate 2024-04-06 17:26:54
*/

@Service
public class ZtyhFundsUserServiceImpl extends ServiceImpl<ZtyhFundsUserMapper, ZtyhFundsUser>
    implements ZtyhFundsUserService{

    @Autowired
    private ZtyhFundsUserMapper ztyhFundsUserMapper;

    @Override
    public boolean addFunds(FundsDto fundsDto) {
        IdWorker idWorker=new IdWorker();
        long l = idWorker.nextId();
        ZtyhFundsUser ztyhFundsUser = new ZtyhFundsUser();
        ztyhFundsUser.setId(l);
        ztyhFundsUser.setFid(fundsDto.getFid());
        ztyhFundsUser.setUid(fundsDto.getUid());
        ztyhFundsUser.setAmount(fundsDto.getAmount());
        int insert = ztyhFundsUserMapper.insert(ztyhFundsUser);
        return insert==1;
    }

    @Override
    public Page<FundsUserVo> findByUser(FundsDto fundsDto) {
        System.out.println("fundsDto==>"+fundsDto);
        return ztyhFundsUserMapper.findByUser(new Page<FundsUserVo>(fundsDto.getPage(),fundsDto.getLimit()),fundsDto);
    }

    @Override
    public List<FundsCount> findbyfunds(Long fundsId) {
        if (Objects.isNull(fundsId)){
            return ztyhFundsUserMapper.findAll();
        }
        return ztyhFundsUserMapper.findbyfunds(fundsId);
    }


    @Override
    public boolean updateFunds(FundsUserDto fundsUserDto) {
        return ztyhFundsUserMapper.updateFunds(fundsUserDto)>=1;
    }

    @Override
    public List<ZtyhFundsUser> findFunds(Long userId, Long fundsId) {
        QueryWrapper<ZtyhFundsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",userId)
                .eq("fid",fundsId)
                .eq("state",0);
        return ztyhFundsUserMapper.selectList(queryWrapper);
    }

    @Override
    public ZtyhFundsUser fundsUserById(Long id) {
        return this.ztyhFundsUserMapper.fundsUserById(id);
    }

    @Override
    public User findUserInfo(Long uid) {
        return this.ztyhFundsUserMapper.findUserInfo(uid);
    }


}




